package com.atguigu.mvc.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author 陆小根
 * date: 2022/05/03
 * Description:
 * web工程的初始化类，用来代替web.xml
 */

public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
  /**
   * 执行Spring的配置类
   * @return
   */
  @Override
  protected Class<?>[] getRootConfigClasses() {
    return new Class[]{SpringConfig.class};
  }

  /**
   * 指定SpringMVC的配置类
   * @return
   */
  @Override
  protected Class<?>[] getServletConfigClasses() {
    return new Class[]{WebConfig.class};
  }

  /**
   * 指定DispatchServlet的映射规则，即url—pattern
   * @return
   */
  @Override
  protected String[] getServletMappings() {
    return new String[]{"/"};
  }

  /**
   * 注册过滤器
   * @return
   */
  @Override
  protected Filter[] getServletFilters() {
    CharacterEncodingFilter  characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceRequestEncoding(true);
    HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
    return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
  }
}
